package com.pope.framework.triage.modular.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pope.framework.core.base.controller.BaseController;
import com.pope.framework.core.reqres.response.ResponseData;
import com.pope.framework.core.util.StringUtil;
import com.pope.framework.dict.modular.entity.DictAllergic;
import com.pope.framework.dict.modular.entity.DictComingChannel;
import com.pope.framework.dict.modular.entity.DictComingWay;
import com.pope.framework.dict.modular.entity.DictComplaints;
import com.pope.framework.dict.modular.entity.DictComplaintsD;
import com.pope.framework.dict.modular.entity.DictConsciousState;
import com.pope.framework.dict.modular.entity.DictEscort;
import com.pope.framework.dict.modular.entity.DictExamination;
import com.pope.framework.dict.modular.entity.DictGreenChannel;
import com.pope.framework.dict.modular.entity.DictMedical;
import com.pope.framework.dict.modular.entity.DictNorm;
import com.pope.framework.dict.modular.entity.DictSicknessGrade;
import com.pope.framework.dict.modular.entity.DictStrength;
import com.pope.framework.dict.modular.entity.DictTriageEpidemic;
import com.pope.framework.dict.modular.entity.DictWhereAbouts;
import com.pope.framework.dict.modular.service.DictAllergicService;
import com.pope.framework.dict.modular.service.DictComingChannelService;
import com.pope.framework.dict.modular.service.DictComingWayService;
import com.pope.framework.dict.modular.service.DictComplaintsDService;
import com.pope.framework.dict.modular.service.DictComplaintsService;
import com.pope.framework.dict.modular.service.DictConsciousStateService;
import com.pope.framework.dict.modular.service.DictEscortService;
import com.pope.framework.dict.modular.service.DictExaminationService;
import com.pope.framework.dict.modular.service.DictGreenChannelService;
import com.pope.framework.dict.modular.service.DictMedicalService;
import com.pope.framework.dict.modular.service.DictNormService;
import com.pope.framework.dict.modular.service.DictSicknessGradeService;
import com.pope.framework.dict.modular.service.DictStrengthService;
import com.pope.framework.dict.modular.service.DictTriageEpidemicService;
import com.pope.framework.dict.modular.service.DictWhereAboutsService;
import com.pope.framework.kernel.model.exception.ServiceException;
import com.pope.framework.kernel.model.page.PageResult;
import com.pope.framework.page.PageFactory;
import com.pope.framework.third.ThirdUtil;
import com.pope.framework.triage.modular.dto.TriageInfoDto;
import com.pope.framework.triage.modular.entity.ComingWay;
import com.pope.framework.triage.modular.entity.ComplaintsInfo;
import com.pope.framework.triage.modular.entity.EscortPerson;
import com.pope.framework.triage.modular.entity.GradeInfo;
import com.pope.framework.triage.modular.entity.GrageChangeLog;
import com.pope.framework.triage.modular.entity.GreenChannel;
import com.pope.framework.triage.modular.entity.PatientAllergic;
import com.pope.framework.triage.modular.entity.PatientInfo;
import com.pope.framework.triage.modular.entity.PatientMedical;
import com.pope.framework.triage.modular.entity.TriageEpidemic;
import com.pope.framework.triage.modular.entity.TriageExamination;
import com.pope.framework.triage.modular.entity.TriageInfo;
import com.pope.framework.triage.modular.entity.TriageNorm;
import com.pope.framework.triage.modular.entity.TriageNormLog;
import com.pope.framework.triage.modular.entity.TriageNormLogTotal;
import com.pope.framework.triage.modular.service.ComingWayService;
import com.pope.framework.triage.modular.service.ComplaintsInfoService;
import com.pope.framework.triage.modular.service.EscortPersonService;
import com.pope.framework.triage.modular.service.GradeInfoService;
import com.pope.framework.triage.modular.service.GrageChangeLogService;
import com.pope.framework.triage.modular.service.GreenChannelService;
import com.pope.framework.triage.modular.service.PatientAllergicService;
import com.pope.framework.triage.modular.service.PatientInfoService;
import com.pope.framework.triage.modular.service.PatientMedicalService;
import com.pope.framework.triage.modular.service.TriageEpidemicService;
import com.pope.framework.triage.modular.service.TriageExaminationService;
import com.pope.framework.triage.modular.service.TriageInfoService;
import com.pope.framework.triage.modular.service.TriageNormLogService;
import com.pope.framework.triage.modular.service.TriageNormLogTotalService;
import com.pope.framework.triage.modular.service.TriageNormService;
import com.pope.framework.triage.modular.vo.ComplaintClassifyVo;
import com.pope.framework.triage.modular.vo.ComplaintVo;
import com.pope.framework.triage.modular.vo.DictGradeVo;
import com.pope.framework.triage.modular.vo.DictVo;
import com.pope.framework.triage.modular.vo.GrageChangeLogVo;
import com.pope.framework.triage.modular.vo.PatientInfoVo;
import com.pope.framework.triage.modular.vo.TriageInfoVo;
import com.pope.framework.triage.modular.vo.TriageNormLogTotalVo;
import com.pope.framework.triage.modular.vo.TriageNormLogVo;
import com.pope.framework.triage.modular.vo.TriageNormVo;
import com.pope.framework.triage.modular.vo.TriageSimpleInfoVo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.ExampleProperty;
import io.swagger.annotations.Example;
@RestController
//@CrossOrigin(allowCredentials="true",maxAge = 3600)
@CrossOrigin
@RequestMapping("${spring.application.app-id}/triage")
@Api(value="分诊业务相关API",tags= {"分诊业务相关API"})
public class TriageInfoController {
	@Autowired
	private TriageInfoService triageInfoService;
	@Autowired
	private PatientInfoService patientInfoService;
	
	@Autowired
	private ComingWayService comingWayService;
	@Autowired
	private EscortPersonService escortPersonService;
	@Autowired
	private GreenChannelService greenChannelService;
	@Autowired
	private ComplaintsInfoService complaintsInfoService;
	@Autowired
	private DictComplaintsService dictComplaintsService;
	
	@Autowired
	private DictComplaintsDService dictComplaintsDService;
	@Autowired
	private DictComingWayService dictComingWayService;
	@Autowired
	private DictEscortService dictEscortService;
	@Autowired
	private DictGreenChannelService dictGreenChannelService;
	@Autowired
	private DictStrengthService dictStrengthService;
	@Autowired
	private DictWhereAboutsService dictWhereAboutsService;
	@Autowired
	private DictConsciousStateService dictConsciousStateService;
	
	@Autowired
	private DictComingChannelService dictComingChannelService;
	@Autowired
	private DictTriageEpidemicService dictTriageEpidemicService;
	@Autowired
	private TriageEpidemicService triageEpidemicService;
	@Autowired
	private TriageNormService triageNormService;
	
	@Autowired
	private DictNormService dictNormService;
	
	@Autowired
	private GradeInfoService gradeInfoService;
	@Autowired
	private GrageChangeLogService grageChangeLogService;
	
	@Autowired
	private DictExaminationService dictExaminationService;
	
	@Autowired
	private DictSicknessGradeService dictSicknessGradeService;
	@Autowired
	private TriageExaminationService triageExaminationService; 
	@Autowired
	private TriageNormLogService triageNormLogService;
	@Autowired
	private TriageNormLogTotalService triageNormLogTotalService;
	@Autowired
	private ThirdUtil thirdUtil;
	@GetMapping("list")
	@ApiOperation(value = "查询分诊信息",notes = "根据姓名或者分诊号等信息查询记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "分诊ID主键",dataType = "Integer",paramType = "query",example = "1112",required=false),
            @ApiImplicitParam(name = "name",value = "患者名称",dataType = "String",paramType = "query",example = "张三"),
            @ApiImplicitParam(name = "beginDate",value = "分诊开始时间，格式为yyyy-MM-dd",dataType = "String",paramType = "query",example = "2018-01-02"),
            @ApiImplicitParam(name = "endDate",value = "分诊结束时间,格式为yyyy-MM-dd",dataType = "String",paramType = "query",example = "2018-01-02"),
            @ApiImplicitParam(name = "emergencyNumber",value = "分诊编号",dataType = "String",paramType = "query",example = "1"),
            @ApiImplicitParam(name = "triageState",value = "分诊状态1待评估2已评估",dataType = "String",paramType = "query",example = "1"),
            @ApiImplicitParam(name = "hospitalId",value = "登陆用户所在医院ID",dataType = "Integer",paramType = "query",example = "1"),
            
            @ApiImplicitParam(name = "pagestart",value = "第几页，从1开始，默认为1",dataType = "String",paramType = "query",example = "1"),
            @ApiImplicitParam(name = "pagenum",value = "每页显示记录数，默认为20",dataType = "String",paramType = "query",example = "10"),
            @ApiImplicitParam(name = "orderby",value = "排序字段，默认为主键ID，可排序字段有id，name患者姓名，depart部门ID，updatetime分诊最后更新时间，CREATETIME分诊创建时间",dataType = "String",paramType = "query",example = "id"),
            @ApiImplicitParam(name = "sort",value = "排序方式，有asc,desc,默认为主键asc",dataType = "String",paramType = "query",example = "desc"),
            
    })
	
	@ApiResponse(code=1,message="返回对象",response=TriageSimpleInfoVo.class,responseContainer="List")
	public ResponseData<PageResult<TriageSimpleInfoVo>> list(Integer id,String name,String beginDate,String endDate,String  emergencyNumber,String triageState,Integer hospitalId){		
		Page<TriageSimpleInfoVo> page=PageFactory.defaultPage();
		
//    	QueryWrapper<TriageInfo> wrapper=new QueryWrapper<TriageInfo>();
//    	//wrapper.setEntity(triageInfo);
		IPage<TriageSimpleInfoVo> data=triageInfoService.listSimple(page, id, name, beginDate, endDate,emergencyNumber,triageState,hospitalId);
		
		return ResponseData.success(PageFactory.pageResult(data));
	}
	
	@ApiOperation(value = "根据ID获取分诊明细",notes = "根据ID获取分诊明细")
    
    @ApiImplicitParam(name = "id",value = "分诊ID主键",dataType = "Integer",paramType = "path",example = "1112",required=true)
	@ApiResponse(code=1,message="返回对象",response=TriageInfoVo.class)
	@GetMapping("{id}")
	public ResponseData<TriageInfoVo> select(@PathVariable Integer id){
		if(id==null) {
			throw new ServiceException("主键ID不能为 NULL");
		}
		List<DictComingWay> dictComingWays=dictComingWayService.list();
		List<DictEscort> dictEscores=dictEscortService.list();
		List<DictGreenChannel>  dictGreenChannels=dictGreenChannelService.list();
		
		List<DictComplaintsD> dictComplaintsD=dictComplaintsDService.list();
		List<DictComplaints>  dictComplaints=dictComplaintsService.list();
		List<DictStrength> dictStrengths=dictStrengthService.list();
		
		List<DictWhereAbouts> dictWhereAbouts=dictWhereAboutsService.list();
		List<DictConsciousState> dictConsciousStates=dictConsciousStateService.list();
		
		List<DictComingChannel> dictComingChannels= dictComingChannelService.list();
		
		List<DictTriageEpidemic> dictTriageEpidemics=dictTriageEpidemicService.list();
		List<DictNorm> dictNorms=dictNormService.list();
		
		List<DictExamination> dictExaminations=dictExaminationService.list();
		
		List<DictSicknessGrade> dictSicknessGrades=dictSicknessGradeService.list();
		
		TriageInfo triageInfo=this.triageInfoService.getById(id);
		if(triageInfo==null) {
			throw new ServiceException("该分诊信息不存在，请重新确认！");
		}
		TriageInfoVo triageInfoVo=new TriageInfoVo();
		BeanUtil.copyProperties(triageInfo, triageInfoVo);
		//获取病人基本信息
		if(triageInfoVo!=null &&triageInfoVo.getPatientId()!=null) {
			PatientInfo patientInfo=patientInfoService.getById(triageInfo.getPatientId());
			if(patientInfo!=null) {
				PatientInfoVo patientInfoVo=patientInfoService.getPatientDisplay(patientInfo);
				triageInfoVo.setPatientInfoVo(patientInfoVo);
			}
		}
		


		//获取检伤依据
		TriageExamination queryTriageExamination=new TriageExamination();
		queryTriageExamination.setTriageId(triageInfo.getId());
		QueryWrapper<TriageExamination> queryTriageExaminationWrapper=new QueryWrapper<>();
		queryTriageExaminationWrapper.setEntity(queryTriageExamination);
		List<TriageExamination> triageExaminations=triageExaminationService.list(queryTriageExaminationWrapper);
		if(CollUtil.isNotEmpty(triageExaminations) && CollUtil.isNotEmpty(dictExaminations)) {
			triageInfoVo.setDictExaminatons(CollUtil.newArrayList());
			triageExaminations.forEach(triageExamination->{
				dictExaminations.forEach(dictExamination->{
					if(triageExamination.getExaminationCode().equals(dictExamination.getCode())) {
						DictGradeVo dictVo=new DictGradeVo();
						BeanUtil.copyProperties(dictExamination, dictVo);
						triageInfoVo.getDictExaminatons().add(dictVo);
					}
					
				});
			});
		}
		//获取病痛等级名称
		if(CollUtil.isNotEmpty(dictSicknessGrades)) {
			for(DictSicknessGrade channel:dictSicknessGrades) {
				if(channel.getCode().equals(triageInfo.getSicknessGrade())) {
					triageInfoVo.setSicknessGradeName(channel.getName());
					break;
				}
			}
		}
		
		//获取流行学病史
		TriageEpidemic queryTriageEpidemic=new TriageEpidemic();
		queryTriageEpidemic.setTriageId(triageInfo.getId());
		QueryWrapper<TriageEpidemic> queryTriageEpidemicWrapper=new QueryWrapper<>();
		queryTriageEpidemicWrapper.setEntity(queryTriageEpidemic);
		List<TriageEpidemic> triageEpidemics=triageEpidemicService.list(queryTriageEpidemicWrapper);
		if(CollUtil.isNotEmpty(triageEpidemics) && CollUtil.isNotEmpty(dictTriageEpidemics)) {
			triageInfoVo.setDictEpidemics(CollUtil.newArrayList());
			triageEpidemics.forEach(triageEpidemic->{
				dictTriageEpidemics.forEach(dictTriageEpidemic->{
					if(triageEpidemic.getEpidemicCode().equals(dictTriageEpidemic.getCode())) {
						DictVo dictVo=new DictVo();
						BeanUtil.copyProperties(dictTriageEpidemic, dictVo);
						triageInfoVo.getDictEpidemics().add(dictVo);
					}
				});
			});
		}
		
		//来院途径
		if(CollUtil.isNotEmpty(dictComingChannels)) {
			for(DictComingChannel channel:dictComingChannels) {
				if(channel.getCode().equals(triageInfo.getComingChannel())) {
					triageInfoVo.setComingChannelName(channel.getName());
					break;
				}
			}
		}
		
		
		//获取来院方式
		ComingWay queryComingWay=new ComingWay();
		queryComingWay.setTriageId(triageInfo.getId());
		QueryWrapper<ComingWay> queryComingWayWrapper=new QueryWrapper<>();
		queryComingWayWrapper.setEntity(queryComingWay);
		List<ComingWay> comingWays=comingWayService.list(queryComingWayWrapper);
		if(CollUtil.isNotEmpty(comingWays) && CollUtil.isNotEmpty(dictComingWays)) {
			triageInfoVo.setDictComingWays(CollUtil.newArrayList());
			comingWays.forEach(comingWay->{
				dictComingWays.forEach(dictComingWay->{
					if(comingWay.getWayId().equals(dictComingWay.getCode())) {
						DictVo dictVo=new DictVo();
						BeanUtil.copyProperties(dictComingWay, dictVo);
						triageInfoVo.getDictComingWays().add(dictVo);
					}
					
				});
			});
		}
		//获取陪送方式
		EscortPerson queryEscortPerson=new EscortPerson();
		queryEscortPerson.setTriageId(triageInfo.getId());
		QueryWrapper<EscortPerson> queryEscortPersonWrapper=new QueryWrapper<>();
		queryEscortPersonWrapper.setEntity(queryEscortPerson);
		
		List<EscortPerson> escortPersons=escortPersonService.list(queryEscortPersonWrapper);
		if(CollUtil.isNotEmpty(escortPersons) && CollUtil.isNotEmpty(dictEscores)) {
			triageInfoVo.setDictEscorts(CollUtil.newArrayList());
			escortPersons.forEach(escortPerson->{
				dictEscores.forEach(dictEscore->{
					if(escortPerson.getEscortId().equals(dictEscore.getCode())) {
						DictVo dictVo=new DictVo();
						BeanUtil.copyProperties(dictEscore, dictVo);
						triageInfoVo.getDictEscorts().add(dictVo);
					}
				});
			});
		}
		
		//获取绿色通道
		GreenChannel queryGreenChannel=new GreenChannel();
		queryGreenChannel.setTriageId(triageInfo.getId());
		QueryWrapper<GreenChannel> queryGreenChannelWrapper=new QueryWrapper<>();
		queryGreenChannelWrapper.setEntity(queryGreenChannel);
		List<GreenChannel> greenChannels=greenChannelService.list(queryGreenChannelWrapper);
		if(CollUtil.isNotEmpty(greenChannels) && CollUtil.isNotEmpty(dictGreenChannels)) {
			triageInfoVo.setDictGreenChanels(CollUtil.newArrayList());
			dictGreenChannels.forEach(dictGreenChannel->{
				greenChannels.forEach(greenChannel->{
					if(dictGreenChannel.getCode().equals(greenChannel.getChannelId())) {
						DictVo dictVo=new DictVo();
						BeanUtil.copyProperties(dictGreenChannel, dictVo);
						triageInfoVo.getDictGreenChanels().add(dictVo);
					}
				});
			});
		}
		
		
		//获取生命特征数
		TriageNorm queryTriageNorm=new TriageNorm();
		queryTriageNorm.setTriageId(triageInfo.getId());
		QueryWrapper<TriageNorm> queryTriageNormWrapper=new QueryWrapper<>();
		queryTriageNormWrapper.setEntity(queryTriageNorm);
		List<TriageNorm> triageNorms=triageNormService.list(queryTriageNormWrapper);
		if(CollUtil.isNotEmpty(triageNorms) && CollUtil.isNotEmpty(dictNorms)) {
			triageInfoVo.setLiveNorms(CollUtil.newArrayList());
			dictNorms.forEach(dictNorm->{
				triageNorms.forEach(triageNorm->{
					if(dictNorm.getCode().equals(triageNorm.getNormId())) {
						TriageNormVo vo=new TriageNormVo();
						vo.setCode(dictNorm.getCode());
						vo.setName(dictNorm.getName());
						vo.setUnit(dictNorm.getUnit());
						vo.setValue(triageNorm.getValue());
						triageInfoVo.getLiveNorms().add(vo);
					}
				});
			});
		}
		//获取主诉
		
		ComplaintsInfo queryComplaintsInfo=new ComplaintsInfo();
		queryComplaintsInfo.setTriageId(triageInfo.getId());
		QueryWrapper<ComplaintsInfo> queryComplaintsInfoWrapper=new QueryWrapper<>();
		queryComplaintsInfoWrapper.setEntity(queryComplaintsInfo);
		List<ComplaintsInfo> complaintsInfos=complaintsInfoService.list(queryComplaintsInfoWrapper);
		List<ComplaintClassifyVo> complaintClassifyVos=CollUtil.newArrayList();
		if(CollUtil.isNotEmpty(complaintsInfos) && CollUtil.isNotEmpty(dictComplaints) && CollUtil.isNotEmpty(dictComplaintsD)) {
			complaintsInfos.forEach(complaintInfo->{
				String classifyCode="";
				ComplaintVo complaintVo=null;
				for(DictComplaintsD dict:dictComplaintsD) {
					if(dict.getCode().equals(complaintInfo.getComplaintsId())) {
						classifyCode=dict.getClassifyCode();
						//BeanUtil.copyProperties(dict, complaintVo);
						if(complaintVo==null) {
							complaintVo=new ComplaintVo();
						}
						complaintVo.setCompaintId(dict.getCode());
						complaintVo.setCompaintName(dict.getName());
						
						break;
					}
				}
				//获取强度
				for(DictStrength dict:dictStrengths) {
					if(dict.getCode().equals(complaintInfo.getStrength())) {
						if(complaintVo==null) {
							complaintVo=new ComplaintVo();
						}
						complaintVo.setStrengthId(dict.getCode());
						complaintVo.setStrengthName(dict.getName());
						break;
					}
				}
				
				
				
				ComplaintClassifyVo existVo=checkcomplaintClassifyHas(complaintClassifyVos,classifyCode);
				if(existVo==null) {
					for(DictComplaints dict:dictComplaints) {
						if(dict.getCode().equals(classifyCode)) {
							existVo=new ComplaintClassifyVo();
							
							existVo.setClassifyId(dict.getCode());
							existVo.setName(dict.getName());
							existVo.setComplaints(CollUtil.newArrayList());
							existVo.getComplaints().add(complaintVo);
							complaintClassifyVos.add(existVo);
							break;
						}
					}
				}else {
					existVo.getComplaints().add(complaintVo);
				}
				
			});
			triageInfoVo.setComplaintClssifys(complaintClassifyVos);
		}
		//分诊去向
		
		if(CollUtil.isNotEmpty(dictWhereAbouts)) {
			for(DictWhereAbouts where:dictWhereAbouts) {
				if(where.getCode().equals(triageInfo.getWhereAbouts())) {
					triageInfoVo.setWhereAboutsName(where.getName());
					break;
				}
			}
		}
		//意识状态
		if(CollUtil.isNotEmpty(dictConsciousStates)) {
			for(DictConsciousState state:dictConsciousStates) {
				if(state.getCode().equals(triageInfo.getConsciousState())) {
					triageInfoVo.setConsciousStateName(state.getName());
					break;
				}
			}
		}
		//获取分级名称
		if(triageInfo.getComingGrade()!=null) {
			GradeInfo gradeInfo=gradeInfoService.getById(triageInfo.getComingGrade());
			if(gradeInfo!=null) {
				triageInfoVo.setComingGradeName(gradeInfo.getName());
				triageInfoVo.setComingGradeInfo(gradeInfo);
			}
		}
		
		if(triageInfo.getChangeGrade()!=null) {
			GradeInfo gradeInfo=gradeInfoService.getById(triageInfo.getChangeGrade());
			if(gradeInfo!=null) {
				triageInfoVo.setChangeGradeName(gradeInfo.getName());
				triageInfoVo.setChangeGradeInfo(gradeInfo);
			}
		}
		
		if(triageInfo.getAutoGrade()!=null) {
			GradeInfo gradeInfo=gradeInfoService.getById(triageInfo.getAutoGrade());
			if(gradeInfo!=null) {
				triageInfoVo.setAutoGradeName(gradeInfo.getName());
			}
		}
		
		//获取科室
		if(triageInfo.getDepart()!=null) {
			Map<String,Object> map=thirdUtil.getDepartById(triageInfo.getDepart());
			if(map!=null) {
				triageInfoVo.setDepartName(StringUtil.toStr(map.get("name")));
			}
		}
		
		
		return ResponseData.success(triageInfoVo);
	}
	
	private ComplaintClassifyVo checkcomplaintClassifyHas(List<ComplaintClassifyVo> complaintClassifyVos,String code) {
		if(CollUtil.isNotEmpty(complaintClassifyVos)) {
			for(ComplaintClassifyVo classify:complaintClassifyVos) {
				if(classify.getClassifyId().equals(code)) {
					return classify;
				}
			}
		}
		return null;
	}
	
	@ApiOperation(value = "添加分诊记录",notes = "添加分诊记录")
	  @ApiImplicitParams({
	@ApiImplicitParam(name="triageInfoDto",dataType="TriageInfoDto", paramType="body",examples = @Example({
        @ExampleProperty(value = "{'user':'id'}", mediaType = "application/json")
    }))
	  })
	@PostMapping("")
	public ResponseData<Integer> add(@RequestBody @Valid TriageInfoDto triageInfoDto,BindingResult bindingResult){
		if(triageInfoDto==null) {
			throw new ServiceException("存储对象为NULL，请重写确认！");
		}
		triageInfoDto.setId(null);
		Integer id=this.triageInfoService.insert(triageInfoDto);
		
		return ResponseData.success(id);
	}
	
	@ApiOperation(value = "修改分诊记录",notes = "修改分诊记录")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "分诊ID主键",dataType = "Integer",paramType = "path",example = "1112",required=true),
		@ApiImplicitParam(required=true,name="triageInfoDto",dataType="TriageInfoDto", paramType="body",examples = @Example({
			@ExampleProperty(value = "{'user':'id'}", mediaType = "application/json")
		}))
	})
	@PutMapping("{id}")
	public ResponseData update(@PathVariable("id") Integer id,@RequestBody  @Valid  TriageInfoDto triageInfoDto,BindingResult bindingResult){		
		if(id==null) {
			throw new ServiceException("主键ID不能为null");
		}
		if(triageInfoDto==null) {
			throw new ServiceException("存储对象为NULL，请重写确认！");
		}
		triageInfoDto.setId(id);
		this.triageInfoService.modify(triageInfoDto);
		return ResponseData.success();
	}
	@ApiOperation(value = "根据ID删除分诊明细",notes = "根据ID删除分诊明细")
    
    @ApiImplicitParam(name = "id",value = "分诊ID主键",dataType = "Integer",paramType = "path",example = "1112",required=true)
	@DeleteMapping("{id}")
	public ResponseData<Integer> delete(@PathVariable("id") Integer id){	
		if(id==null) {
			throw new ServiceException("主键ID不能为null");
		}
		this.triageInfoService.delete(id);
		return ResponseData.success(id);
	}
	@GetMapping("{triageId}/livenorms/logs/list")
	@ApiOperation(value = "根据分诊ID获取生命体征变更日志",notes = "根据分诊ID获取生命体征变更日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "triageId",value = "分诊ID主键",dataType = "Integer",paramType = "path",example = "1112",required=true),
            
            @ApiImplicitParam(name = "pagestart",value = "第几页，从1开始，默认为1",dataType = "String",paramType = "query",example = "1"),
            @ApiImplicitParam(name = "pagenum",value = "每页显示记录数，默认为20",dataType = "String",paramType = "query",example = "10"),
            
    })
	public ResponseData<PageResult<TriageNormLogTotalVo>> listLiveNorms(@PathVariable("triageId")Integer triageId){
		if(triageId==null) {
			throw new ServiceException("分诊ID不能为空！");
		}
		TriageInfo triageInfo=triageInfoService.getById(triageId);
		if(triageInfo==null) {
			throw new ServiceException("该分诊信息已不存在！");
		}
		Page<TriageNormLogTotal> page=PageFactory.defaultPage();
		TriageNormLogTotal queryTriageNormLogTotal=new TriageNormLogTotal();
		queryTriageNormLogTotal.setTriageId(triageId);
		QueryWrapper<TriageNormLogTotal> queryTriageNormLogTotalWrapper=new QueryWrapper<>();
		queryTriageNormLogTotalWrapper.setEntity(queryTriageNormLogTotal);
		queryTriageNormLogTotalWrapper.orderByDesc("UPDATETIME");
		IPage<TriageNormLogTotal> datas=triageNormLogTotalService.page(page, queryTriageNormLogTotalWrapper);
		List<TriageNormLogTotalVo> vos=CollUtil.newArrayList();
		QueryWrapper<DictNorm> wrapper=new QueryWrapper<>();
		wrapper.orderByAsc("orders");
		List<DictNorm> dicts=dictNormService.list(wrapper);
		if(datas!=null && CollUtil.isNotEmpty(datas.getRecords())) {
			for(TriageNormLogTotal triageNormLogTotal:datas.getRecords()) {
				
				TriageNormLogTotalVo triageNormLogTotalVo=new TriageNormLogTotalVo();
				BeanUtil.copyProperties(triageNormLogTotal, triageNormLogTotalVo);
				vos.add(triageNormLogTotalVo);
				
				TriageNormLog queryTriageNormLog=new TriageNormLog();
				queryTriageNormLog.setLogId(triageNormLogTotal.getId());
				QueryWrapper<TriageNormLog> queryTriageNormLogWrapper=new QueryWrapper<TriageNormLog>();
				queryTriageNormLogWrapper.setEntity(queryTriageNormLog);
				
				List<TriageNormLog> logs=triageNormLogService.list(queryTriageNormLogWrapper);
				List<TriageNormLogVo> logVos=CollUtil.newArrayList();
				triageNormLogTotalVo.setNormLogs(logVos);
				if(CollUtil.isNotEmpty(dicts)) {
					for(DictNorm dictNorm:dicts) {
						boolean find=false;
						if(CollUtil.isNotEmpty(logs)) {
							for(TriageNormLog log:logs) {
								if(dictNorm.getCode().equals(log.getNormId())) {
									find=true;
									TriageNormLogVo logVo=new TriageNormLogVo();
									BeanUtil.copyProperties(log, logVo);
									logVos.add(logVo);
									logVo.setUnit(dictNorm.getUnit());
									logVo.setNormName(dictNorm.getName());
									break;
								}
							}
						}
						if(!find) {
							TriageNormLogVo logVo=new TriageNormLogVo();
							logVo.setNormId(dictNorm.getCode());
							logVos.add(logVo);
							logVo.setUnit(dictNorm.getUnit());
							logVo.setNormName(dictNorm.getName());
						}
					}
				}
			}
		}
		
		IPage<TriageNormLogTotalVo> pages=new Page<TriageNormLogTotalVo>();
		pages.setCurrent(datas.getCurrent());
		pages.setPages(datas.getPages());
		pages.setRecords(vos);
		pages.setSize(datas.getSize());
		pages.setTotal(datas.getTotal());
		return ResponseData.success(PageFactory.pageResult(pages));
	}
	
	
	@GetMapping("{triageId}/grade/logs/list")
	@ApiOperation(value = "根据分诊ID获取分诊等级变更日志",notes = "根据分诊ID获取分诊等级变更日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "triageId",value = "分诊ID主键",dataType = "Integer",paramType = "path",example = "1112",required=true),
            
            @ApiImplicitParam(name = "pagestart",value = "第几页，从1开始，默认为1",dataType = "String",paramType = "query",example = "1"),
            @ApiImplicitParam(name = "pagenum",value = "每页显示记录数，默认为20",dataType = "String",paramType = "query",example = "10"),
            
    })
	
	
	
	public ResponseData<PageResult<GrageChangeLogVo>> listGrade(@PathVariable("triageId")Integer triageId){		
		if(triageId==null) {
			throw new ServiceException("分诊ID不能为空！");
		}
		TriageInfo triageInfo=triageInfoService.getById(triageId);
		if(triageInfo==null) {
			throw new ServiceException("该分诊信息已不存在！");
		}
		Page<GrageChangeLog> page=PageFactory.defaultPage();
		GrageChangeLog queryGrageChangeLog=new GrageChangeLog();
		queryGrageChangeLog.setTriageId(triageId);
		QueryWrapper<GrageChangeLog> queryGrageChangeLogWrapper=new QueryWrapper<>();
		queryGrageChangeLogWrapper.setEntity(queryGrageChangeLog);
		queryGrageChangeLogWrapper.orderByDesc("UPDATETIME");
		IPage<GrageChangeLog> datas=grageChangeLogService.page(page, queryGrageChangeLogWrapper);
		List<GrageChangeLogVo> vos=CollUtil.newArrayList();
		if(datas!=null && CollUtil.isNotEmpty(datas.getRecords())) {
			for(GrageChangeLog gradeChangeLog:datas.getRecords()) {
				GrageChangeLogVo vo=new GrageChangeLogVo();
				BeanUtil.copyProperties(gradeChangeLog, vo);
				vos.add(vo);
				if(vo.getOldGradeCode()!=null) {
					GradeInfo queryGradeInfo=new GradeInfo();
					queryGradeInfo.setId(vo.getOldGradeCode());
					QueryWrapper<GradeInfo> queryGradeInfoWrapper=new QueryWrapper<>();
					queryGradeInfoWrapper.setEntity(queryGradeInfo);
					List<GradeInfo> gradeInfos=gradeInfoService.list(queryGradeInfoWrapper);
					if(gradeInfos!=null && gradeInfos.size()>0) {
						vo.setOldGradeCodeName(gradeInfos.get(0).getName());
						vo.setOldGradeInfo(gradeInfos.get(0));
					}
				}
				if(vo.getNewGradeCode()!=null) {
					GradeInfo queryGradeInfo=new GradeInfo();
					queryGradeInfo.setId(vo.getNewGradeCode());
					QueryWrapper<GradeInfo> queryGradeInfoWrapper=new QueryWrapper<>();
					queryGradeInfoWrapper.setEntity(queryGradeInfo);
					List<GradeInfo> gradeInfos=gradeInfoService.list(queryGradeInfoWrapper);
					if(gradeInfos!=null && gradeInfos.size()>0) {
						vo.setNewGradeCodeName(gradeInfos.get(0).getName());
						vo.setNewGradeInfo(gradeInfos.get(0));
					}
				}
//				TriageNormLog queryTriageNormLog=new TriageNormLog();
//				queryTriageNormLog.setLogId(gradeChangeLog.getId());
//				QueryWrapper<TriageNormLog> queryTriageNormLogWrapper=new QueryWrapper<>();
//				queryTriageNormLogWrapper.setEntity(queryTriageNormLog);
//				List<TriageNormLog> logDatas=triageNormLogService.list(queryTriageNormLogWrapper);
//				List<TriageNormLogVo> logVos=CollUtil.newArrayList();
//				vo.setTriageNormLog(logVos);
//				if(CollUtil.isNotEmpty(logDatas)) {
//					List<DictNorm> dictNorms=dictNormService.list();
//					for(TriageNormLog log:logDatas) {
//						TriageNormLogVo logVo=new TriageNormLogVo();
//						logVos.add(logVo);
//						BeanUtil.copyProperties(log, logVo);
//						
//						if(CollUtil.isNotEmpty(dictNorms)) {
//							for(DictNorm dictNorm:dictNorms) {
//								if(dictNorm.getCode().equals(log.getNormId())) {
//									logVo.setUnit(dictNorm.getUnit());
//									logVo.setNormName(dictNorm.getName());
//								}
//							}
//						}
//						
//						
//					}
//				}
				
			}
			
		}
		IPage<GrageChangeLogVo> pages=new Page<GrageChangeLogVo>();
		pages.setCurrent(datas.getCurrent());
		pages.setPages(datas.getPages());
		pages.setRecords(vos);
		pages.setSize(datas.getSize());
		pages.setTotal(datas.getTotal());
		return ResponseData.success(PageFactory.pageResult(pages));
	}
	
	
}
